package com.ecb.business.service.impl;

import com.ecb.business.dto.user.QueryUserDTO;
import com.ecb.business.dto.user.UserDTO;
import com.ecb.business.exception.BusinessException;
import com.ecb.business.po.ChainUserPO;
import com.ecb.business.repository.UserDao;
import com.ecb.business.service.UserManageService;
import com.ecb.business.utils.JsonUtils;
import com.ecb.business.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class UserManageServiceImpl implements UserManageService {
    @Autowired
    UserDao userDao;
    @Autowired
    HttpServletRequest request;
    @Override
    public List<UserDTO> getAllUser(QueryUserDTO queryDTO) {
        List<ChainUserPO> allByUserCodeLikeOrUserNameLikeAndStatusEquals = userDao.findAllByUserCodeLikeOrUserNameLikeAndStatusEquals(queryDTO.getUserCode(), queryDTO.getUserName(), 1);
        List<UserDTO> userDTOS = JsonUtils.beanToArray(allByUserCodeLikeOrUserNameLikeAndStatusEquals, UserDTO.class);
        return userDTOS;
    }

    @Override
    public boolean addUser(UserDTO addDTO) {
        ChainUserPO UserPO = new ChainUserPO();
        UserPO.setStatus(1);
        UserPO.setUserCode(addDTO.getUserCode());
        UserPO.setUserName(addDTO.getUserName());
        UserPO.setPassword(addDTO.getPassword());
        UserPO.setUserRole(addDTO.getUserRole());
        UserPO.setCreateTime(new Date());
        UserPO.setModifyTime(new Date());
        userDao.save(UserPO);
        return true;
    }

    @Override
    public boolean checkIsAdmin() {
        String userCode = request.getAttribute("userCode").toString();
        ChainUserPO byUserCode = userDao.findByUserCode(userCode);
        if (byUserCode == null){
            return false;
        }
        if (!byUserCode.getUserRole().equals("admin")){
            return false;
        }
        return true;
    }
    @Override
    public boolean checkIsAdminElseThrow() {
        String userCode = request.getAttribute("userCode").toString();
        ChainUserPO byUserCode = userDao.findByUserCode(userCode);
        if (byUserCode == null){
            throw new BusinessException(500, "用户不存在");
        }
        if (!byUserCode.getUserRole().equals("admin")){
            throw new BusinessException(500, "非管理员用户");
        }
        return true;
    }

    @Override
    public boolean userLogin(UserDTO addDTO) {
        ChainUserPO byUserCode = userDao.findByUserCode(addDTO.getUserCode());
        if (byUserCode == null){
            throw new BusinessException(500, "用户不存在");
        }
        if (!byUserCode.getPassword().equals(addDTO.getPassword())){
            throw new BusinessException(500, "密码错误");
        }
        byUserCode.setModifyTime(new Date());
        ChainUserPO userPO = JsonUtils.beanToBean(byUserCode, ChainUserPO.class);
        // 生成token
        Map<String, String> map = new HashMap<>();
        map.put("userCode", userPO.getUserCode());
        String jwt = JwtUtils.createJwt(userPO.getUserCode(), map);

        userPO.setUserToken(jwt);
        userDao.save(userPO);
        return true;
    }

    @Override
    public boolean updateUser(UserDTO UserDTO) {
        Optional<ChainUserPO> byId = userDao.findById(UserDTO.getId());
        if (!byId.isPresent()){
            throw new BusinessException(500, "用户不存在");
        }
        ChainUserPO UserPO = byId.get();
        UserPO.setId(UserDTO.getId());
        UserPO.setUserName(UserDTO.getUserName());
        userDao.save(UserPO);
        return true;
    }

    @Override
    public boolean deleteUser(UserDTO UserDTO) {
        Optional<ChainUserPO> byId = userDao.findById(UserDTO.getId());
        if (!byId.isPresent()){
            throw new BusinessException(500, "用户不存在");
        }
        ChainUserPO UserPO = byId.get();
        UserPO.setStatus(9); // 已注销
        UserPO.setModifyTime(new Date());
        userDao.save(UserPO);
        return true;
    }
}
